package com.zhongxiangspace.controller;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongxiangspace.entity.Permissions;
import com.zhongxiangspace.entity.VO.PermissionsVO;
import com.zhongxiangspace.service.PermissionsService;
import com.zhongxiangspace.tools.FileUtil;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 首页模块
 */
@Controller
@RequestMapping
public class IndexController {

    @Autowired
    private ReadProperties readProperties;
    @Autowired
    private PermissionsService permissionsService;

    @GetMapping("index.do")
    public Object index(HttpServletRequest request, Model model) {
        List<PermissionsVO> permissionsVOList = this.permissionsService.queryAllByID(0);
        model.addAttribute("object", new Gson().toJson(permissionsVOList));
        return "main/main";
    }
    @GetMapping("/login.do")
    public String login(HttpServletRequest request, Model model) {
        return "adminLogin";
    }

    @GetMapping("img/uploader.do")
    public String uploader(HttpServletRequest request, Model model) {

        return "tools/imgupload";
    }

    @PostMapping("img/uploaderImg.do")
    @ResponseBody
    public Object uploaderImg(@RequestParam("file") MultipartFile multipartFile, Model model) throws IOException {
        Map<String, Object> imagePathMap = null;
        if (!multipartFile.isEmpty() && multipartFile.getContentType().contains("image/")) {
            FileInputStream in = (FileInputStream) multipartFile.getInputStream();
            BufferedImage srcImage = javax.imageio.ImageIO.read(in);
            imagePathMap = FileUtil.saveFile(multipartFile, "test/y", this.readProperties.getImagePath());
        }
        return imagePathMap;
    }


}
